# coding:utf-8
# @Time    : 2021/11/29 17:38
# @Author  : Martin
from page_object.base import PageItems
from page_object.base import BasePage
from util.read_file import get_approve_data


class OnlineOrderReduce(BasePage):
    _base_url = "/nspas-web-in/orderPlummetCreate.htm"

    # def __init__(self, driver, logger):
    #
    #     _base_url = "/nspas-web-in/orderPlummetCreate.htm"
    #     super().__init__(driver, logger, _base_url)

    def select_company(self, txt):
        """
        :param txt: 公司类型
        :return:
        """
        self.select('id', 'activityChannel', '2')
        self.select('id', 'comType', txt)
        self.click('link text', '确定')
        return OnlineOrderReduceCreate(self.driver, self.logger, txt)


class OnlineOrderReduceCreate(PageItems):
    def __new__(cls, *args, **kwargs):

        instance = super().__new__(cls)
        instance._base_url = "/nspas-web-in/activityCreateFilter.do?activityType" \
                             f"=ORDERACTIVITY&promotionActType=ORDERPLUMMET&activityChannel=2&comType={args[2]}"
        return instance

    def __init__(self, driver, logger,com_type):
        # self.com_type = com_type
        # _base_url = "/nspas-web-in/activityCreateFilter.do?activityType=ORDERACTIVITY" \
        #             f"&promotionActType=ORDERPLUMMET&activityChannel=2&comType={self.com_type}"

        super().__init__(driver, logger)

    def input_reduce_rule(self, args: list):

        """
        输入直降优惠信息
        al_type: 计算方式 满=3 每=2 第=4
        cal_limit: 计算门槛值
        unit: 计算单位 元=1 件=3
        amount: 优惠金额
        max_amount: 最大优惠金额
        :param args: 参数列表
        """
        for arg in args:
            cal_type, cal_limit, unit, mode, amount = arg.split(',')
            table = self.locate('css selector', '#actRuleAndLimit')
            self.scroll_ele(table)
            index = args.index(arg) + 1

            self.select('css selector', f'#detailinfoOrderPlummetRule>#detailedtableOrderPlummetRule:nth-child({index})'
                                        ' select[name="orderPlummetRule.calculationMode"]', cal_type, type_="text")
            self.input('css selector', f'#detailinfoOrderPlummetRule>#detailedtableOrderPlummetRule:nth-child({index})'
                                       ' input[name="orderPlummetRule.privilegeSill"]', cal_limit)
            self.select('css selector', f'#detailinfoOrderPlummetRule>#detailedtableOrderPlummetRule:nth-child({index})'
                                        ' select[name="orderPlummetRule.unit"]', unit, type_="text")
            self.select('css selector', f'#detailinfoOrderPlummetRule>#detailedtableOrderPlummetRule:nth-child({index})'
                                        ' select[name="orderPlummetRuleApply.bonusMode"]', mode, type_="text")
            self.input('css selector', f'#detailinfoOrderPlummetRule>#detailedtableOrderPlummetRule:nth-child({index})'
                                       ' input[name="orderPlummetRule.plummet"]', amount)
            if index != len(args):
                self.click('xpath', '//a[@onclick="orderPlummetAdd();setOnlineRiskChangeFlag();"]')

    def input_xn_rule(self, args: list):

        """
        输入直降优惠信息
        al_type: 计算方式 满=3 每=2 第=4
        cal_limit: 计算门槛值
        unit: 计算单位 元=1 件=3
        amount: 优惠金额
        max_amount: 最大优惠金额
        :param args: 参数列表
        """

        for arg in args:
            amount, cal_limit = arg.split(',')
            self.input('name', 'orderPlummetRule.plummet', amount)
            self.input('name', 'orderPlummetRule.privilegeSill', cal_limit)

    def select_reduce_type(self, type_):
        """
        选择直降类型
        :param type_:1：每满第，2：x元N件
        """
        self.click('xpath', f'//input[@name="ruleOption"][@value={type_}]')

    def input_max_min_limit(self, limit: str):
        """
        输入上下限限制
        :param limit: 逗号分隔，ex：上限，下限
        :return:
        """
        max_limit, min_limit = limit.split(',')
        self.click('xpath', f'//input[@value="1"][@name="limitCondition.cmmdtyNumFlag"]')
        self.input('id', 'maxLimit', max_limit)
        self.input('id', 'minLimit', min_limit)


    def input_max_amount(self, txt):
        """
        输入最大金额
        :param txt:  最大金额
        :return:
        """
        self.click('xpath', '//input[@name="maxMoneyLimitRiao"][@value="1"]')
        max_amount = str(txt).split(',')
        for maxi in max_amount:
            index = max_amount.index(maxi) + 1
            self.input('css selector', f'#detailinfoOrderPlummetRule>#detailedtableOrderPlummetRule:nth-child({index})'
                                       ' input[name="orderPlummetRule.ticketPriceMax"]', maxi)


    @staticmethod
    def get_approve_head_create():
        """
        获取总部新建审批流账号数据
        :return:
        """
        return get_approve_data('online_order_reduce', 'head', 'create')

    def go_to_approve(self):
        self.click('xpath', '//input[@onclick="resourceConfirmOk();"]')
        # 点击资源管控确认
        self.click('id', 'comfireBtn')

if __name__ == '__main__':
    pass
